package com.tinghua.mqTest.basices;

import com.rabbitmq.client.*;
import com.tinghua.mqTest.utils.ConnectionMQ;
import com.tinghua.mqTest.utils.SleepUtil;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

/**
 * 简单入门用法，设置basicQos为1表示能者多劳
 */
public class Consumer {
    public static final String QUEUE_NAME = "rabbitmq_01_test";

    public static void main(String[] args) throws IOException, TimeoutException {
        Channel channel = ConnectionMQ.utilConnectionMQ();
        DeliverCallback deliverCallback = (consumerTag, message) -> {
            SleepUtil.sleep(1);
            System.out.println("贵的服务器接收到的消息:"+new String(message.getBody()));
            channel.basicAck(message.getEnvelope().getDeliveryTag(), false);
        };
        CancelCallback cancelCallback = consumerTag -> {
            System.out.println("回调失败!" + consumerTag);
        };
        channel.basicQos(1);
        boolean autoAck = false;
        channel.basicConsume(QUEUE_NAME, deliverCallback, cancelCallback);


    }
}
